using FytSoa.Domain.Crm;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Crm;

/// <summary>
/// 客户回访记录服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v7")]
public class CrmCustomerVisitService : IApplicationService 
{
    private readonly SugarRepository<CrmCustomerVisit> _thisRepository;
    public CrmCustomerVisitService(SugarRepository<CrmCustomerVisit> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<CrmCustomerVisitDto>> GetPagesAsync(PageParam param)
    {
        var queryQuery = _thisRepository.AsQueryable();
        if (!string.IsNullOrEmpty(param.Query))
        {
            var jsonParam=_thisRepository.Context.Utilities.JsonToConditionalModels(param.Query);
            queryQuery.Where(jsonParam);
        }
        var query = await queryQuery
            .WhereIF(!string.IsNullOrEmpty(param.Key),m=>m.Title.Contains(param.Key) || m.Remark.Contains(param.Key))
            .WhereIF(param.Id!=0,m=>m.CustomerId==param.Id)
            .Includes(m=>m.Customer)
            .Includes(m=>m.Contact)
            .Includes(m=>m.Employee)
            .Includes(m=>m.VisitForm)
            .Includes(m=>m.Satisfied)
            .Includes(m=>m.Project)
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<CrmCustomerVisitDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<CrmCustomerVisitDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m=>m.Customer)
            .Includes(m=>m.Employee)
            .Includes(m=>m.Contact)
            .Includes(m=>m.Project)
            .FirstAsync(m=>m.Id==id);
        return model.Adapt<CrmCustomerVisitDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(CrmCustomerVisitDto model)
    {
        var number = string.Empty;
        var toDay = await _thisRepository.CountAsync(m=>SqlFunc.DateIsSame(m.CreateTime,DateTime.Now));
        number = toDay switch
        {
            < 10 => "00" + (toDay + 1),
            >= 10 and < 100 => "0" + (toDay + 1),
            _ => number
        };
        if (toDay>=99)
        {
            number = (toDay+1).ToString();
        }
        model.Number = "HF"+DateTime.Now.ToString("yyyyMMdd")+"-"+number;
        return await _thisRepository.InsertAsync(model.Adapt<CrmCustomerVisit>());
    }
        

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(CrmCustomerVisitDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<CrmCustomerVisit>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
